var STip = ZFT.Util.STip;
var ParseTemplate = ZFT.Util.ParseTemplate;
var Ajax = ZFT.Util.Ajax;
require("./index.scss");
var Template = {
    modal: ParseTemplate(require("./tpl/resetmodal.xtpl")),
    nextstep: ParseTemplate(require("./tpl/nextstep.xtpl")),
    loginBox: ParseTemplate(require("./tpl/loginbox.xtpl"))
}
var Main = ZFT.Util.Class({
    container: $("body"),
    timer: null,
    timer_login: null,
    RESEND_VCODE_TIME: 120,
    EVENTS: {
        "click #loginBtn": "login",
        "click #schBtn": "search",
        "keyup #schInp": "doSearch",
        "keyup #pwd,#user,#verifyInp": "doLogin",
        "click #forgetPwd": "showResetPwdModal",
        "click .cancel,.close": "closeModal",
        "click #sendCodeBtn,#sendResetCodeBtn": "sendCode",
        "click #toggleLoginBtn": "toggleLoginType"
    },
    init: function () {
        var _this = this;
        _this.initLoginBox();
    },
    toggleLoginType: function (e) {
        var _this = this,
            tarBtn = $(e.currentTarget),
            type = tarBtn.attr("data-type");
        tarBtn.attr('data-type', (type == 1) ? 2 : 1);
        $("#toggleIcon").removeClass((type == 1) ? "icon-login-phone" : "icon-login-account").addClass((type == 1) ? "icon-login-account" : "icon-login-phone");
        _this.initLoginBox();
    },
    initLoginBox: function () {
        var _this = this,
            type = $("#toggleLoginBtn").attr("data-type"),
            loginBox = Template.loginBox({ type: type });
        $("#loginContent").html(loginBox);
        (type == 1) && _this.veryfiyCode();
    },
    doSearch: function (e) {
        var keyCode = e.keyCode;
        var _this = this;
        if (keyCode != 13) return false;
        _this.search();
    },
    doLogin: function (e) {
        var keyCode = e.keyCode;
        var _this = this;
        if (keyCode != 13) return false;
        _this.login();
    },
    login: function () {
        var _this = this;
        var param;
        var regTel = /^1\d{10}$/;
        var regCode = /^\d{6}$/;
        var type = $("#toggleLoginBtn").attr("data-type");
        if (type == 1) {
            var username = $("#user").val();
            var password = $("#pwd").val();
            var schInp = $("#schInp").val();
            var verfiyCode = $("#verifyInp").val();
            if (!username) return STip("fail", "用户名不为空!", 4000);
            if (!password) return STip("fail", "密码不为空!", 4000);
            if (!verfiyCode) return STip("fail", "验证码不为空!", 4000);
            param = {
                username: $.trim(username),
                password: hex_md5(hex_md5(password)),
                code: $.trim(verfiyCode),
                type: type
            }
        } else if (type == 2) {
            var mobile = $("#mobileInp").val();
            var code = $("#codeInp").val();
            if (!regTel.test(mobile)) return STip("fail", "请输入11位正确的手机号", 4000);
            if (!regCode.test(code)) return STip("fail", "请输入6位数字验证码", 4000);
            param = {
                mobile: mobile,
                code: code,
                type: type
            }
        }else{
            return STip("fail", "请求出错，请稍候再试！");
        }
        Ajax("/index/index/login_action", {
            type: "POST",
            dataType: "json",
            params: param,
            success: function (res) {
                if (res.code != 200) {
                    return STip("fail", res.msg, 4000);
                } else {
                    /* var dtype = res.data.dtype,
                         url;*/
                    //url = (dtype == 2) ? "/index/index/usercenter" : "/index/index/home";
                    var url = "/index/index/usercenter";
                    schInp ? window.location.href = url + "/?word=" + schInp : window.location.href = url;
                }
            }
        })
    },

    search: function () {
        var schInpVal = document.getElementById('schInp').value;
        if ($.trim(schInpVal) === "") {
            return false;
        } else {
            window.location.href = "/index/index/home?word=" + schInpVal;
        }

    },
    veryfiyCode: function () {
        var _this = this;
        var vImg = $("#verifyImg");
        var link;
        link = "/r/VerifyCode_getCode?" + Math.random();
        vImg.attr("src", link);
        _this.refreshCode();
    },
    refreshCode: function () {
        var _this = this;
        var rBtn = $("#refreshBtn");
        rBtn.on("click", function () {
            _this.veryfiyCode();
        })
    },
    showResetPwdModal: function () {
        var _this = this;
        var modal = $("#modal");
        var html = Template.modal();
        modal.html(html);
        _this.toNextStep();
    },
    sendCode: function (e) {
        var _this = this;
        var tarBtn = $(e.currentTarget);
        var type = tarBtn.attr("data-type");
        var regTel = /^1\d{10}$/;
        var url = {
            "0": "/r/VerifyCode_sendLoginCode",
            "1": "/r/VerifyCode_sendResetCode"
        }
        var timer = (type == 0) ? _this.timer : _this.timer_login;
        var mobile = tarBtn.parent().siblings(".tel.code").children("#mobileInp").val();
        if (!regTel.test(mobile)) return STip("fail", "请输入11位手机号码！");
        Ajax(url[type], {
            type: "POST",
            dataType: "json",
            params: {
                mobile: mobile
            },
            success: function (res) {
                if (res.code == 200) {
                    var last_time = _this.RESEND_VCODE_TIME;
                    STip("success", res.msg, 4000);
                    clearInterval(timer);
                    tarBtn.removeClass("m-btn_ol_default").text(last_time + "秒后重发");
                    _this.timer = setInterval(function () {
                        if (last_time == 0) {
                            tarBtn.addClass("m-btn_ol_default").text("获取验证码").removeAttr("disabled");
                            return clearInterval(timer);
                        }
                        tarBtn.attr("disabled", "disabled");
                        last_time--;
                        tarBtn.text(last_time + "秒后重发");
                    }, 1000);
                } else {
                    clearInterval(timer);
                    tarBtn.addClass("m-btn_ol_default").text("获取验证码").removeAttr("disabled");
                    return STip("fail", res.msg, 4000);
                }
            }
        });


    },
    toNextStep: function () {
        var regTel = /^1\d{10}$/;
        var regCode = /^\d{6}$/;
        $("#nextBtn").on("click", function () {
            var mobile = $("#mobileInp").val();
            var code = $("#codeInp").val();
            if (!regTel.test(mobile)) return STip("fail", "请输入11位手机号码！");
            if (!regCode.test(code)) return STip("fail", "请输入6位数字验证码！");
            Ajax("/r/VerifyCode_checkReset", {
                type: "POST",
                dataType: "json",
                params: {
                    mobile: mobile,
                    mobile_code: code
                },
                success: function (res) {
                    if (res.code == 200) {
                        var next = Template.nextstep({ mobile: mobile });
                        $("#modifyStepBox").html(next);
                        $("#savePwdBtn").on("click", function (e) {
                            var tarBtn = $(e.currentTarget);
                            var mobile = tarBtn.attr("data-t");
                            var newPwd = $("#newPwd").val();
                            var confPwd = $("#confPwd").val();
                            if (!newPwd || !confPwd) return STip("fail", "请输入密码！");
                            Ajax("/r/Member_resetPassword", {
                                type: "POST",
                                dataType: "json",
                                params: {
                                    mobile: mobile,
                                    new_pwd: newPwd,
                                    confirm_pwd: confPwd
                                },
                                loading: function () {
                                    tarBtn.attr("disabled", "disabled").removeClass("m-btn_primary");
                                },
                                success: function (res) {
                                    if (res.code == 200) {
                                        STip("success", res.msg);
                                        window.location.reload();
                                    } else {
                                        STip("fail", res.msg);
                                        tarBtn.removeAttr("disabled").addClass("m-btn_primary");
                                    }
                                }
                            })
                        })
                    } else {
                        STip("fail", res.msg);
                    }
                }
            })

        })
    },
    //关闭模态框并重置计时
    closeModal: function (e) {
        var _this = this;
        var tarBtn = $(e.currentTarget);
        tarBtn.parents(".m-modal_bg").fadeOut(200);
        clearInterval(_this.timer);
    }
})

$(function () {
    new Main();
})